﻿using System;
using System.Reflection;

namespace NetBay.Core.DataAccess
{
    public class StoredProcedureResult
    {

        private string _errorMessage = string.Empty;

        public string Error
        {
            get
            {
                if(string.IsNullOrEmpty(_errorMessage))
                {
                    return GetErrorMessage();
                }
                return _errorMessage;
            }
            set
            {
                _errorMessage = value;
            }
        }

        /// <summary>
        /// Gets the result code.
        /// </summary>
        public ResultCode ResultCode { get; internal set; }

        /// <summary>
        /// Gets a value indicating whether this instance has error.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance has error; otherwise, <c>false</c>.
        /// </value>
        public bool HasError
        {
            get { return Convert.ToInt32(ResultCode) < 0; }
        }

        /// <summary>
        /// Gets or sets the new identifier.
        /// </summary>
        /// <value>
        /// The new identifier.
        /// </value>
        public long NewIdentifier { get; set; }

        /// <summary>
        /// Gets the error message.
        /// </summary> 
        /// <returns></returns>
        public string GetErrorMessage()
        {
            ErrorMessageAttribute da;
            FieldInfo fi = ResultCode.GetType().GetField(ResultCode.ToString());
            da = (ErrorMessageAttribute)Attribute.GetCustomAttribute(fi, typeof(ErrorMessageAttribute));
            if (da != null)
            {
                return da.ErrorMessage;
            }
            return string.Empty;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StoredProcedureResult"/> class.
        /// </summary>
        /// <param name="newIdentifier">The new identifier.</param>
        public StoredProcedureResult(int newIdentifier)
        {
            NewIdentifier = newIdentifier;
            ResultCode = ResultCode.Executed;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StoredProcedureResult"/> class.
        /// </summary>
        /// <param name="resultCode">The result code.</param>
        public StoredProcedureResult(ResultCode resultCode)
        {
            ResultCode = resultCode;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StoredProcedureResult"/> class.
        /// </summary>
        /// <param name="resultCode"></param>
        /// <param name="error"></param>
        public StoredProcedureResult(ResultCode resultCode, string error)
            :this(resultCode)
        {
            _errorMessage = error;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StoredProcedureResult"/> class.
        /// </summary>
        /// <param name="resultCode">The result code.</param>
        public StoredProcedureResult(string resultCode)
        {
            ResultCode = (ResultCode)Enum.Parse(typeof(ResultCode), resultCode, false);
        }

        /// <summary>
        /// Gets the default.
        /// </summary>
        /// <returns></returns>
        public static StoredProcedureResult GetDefault()
        {
            return new StoredProcedureResult(ResultCode.Executed);
        }

    }
}
